package CustomUI;

import org.Consts;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class ydButton extends ImageButton {
	
	

	private ImageButton btn;
	
	boolean toggle=false;
	boolean selected=false;
	
	ydOnClickListeners listeners = new ydOnClickListeners();
	ydOnClickLongListeners longListeners = new ydOnClickLongListeners();

	public ydButton(Context context) {
		super(context);
		init();

	}
	
	public ydButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ydButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	public ydButton(Context context, String tag) {
		super(context);
		setTag(tag);
		init();
	}

	public void init(){
		btn=this;
		
		String tag=(String) this.getTag();
		
		JSONObject obj = null;	
		try {
			Log.d("ydButton Tag", tag);
			obj = new JSONObject(tag);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(obj.optString(Consts.VIEW_TAGS_TOGGLE).equals("true"))
			toggle=true;
		
		listeners.addOnClickListener(new View.OnClickListener(){
			   @Override
			   public void onClick(View v){
				   if(toggle==true){
					   if(selected==true)
						   setState(false);
					   else
						   setState(true);
					   
					   //btn.setSelected(selected);
					   
					   Log.d("listeners", "Working");

				   }
			   }
			});
		
		this.setOnClickListener(listeners);
		this.setOnLongClickListener(longListeners);
		Log.d("listeners", "set on click");
	}

	public void addOnClickListener(OnClickListener onClickListener) {
		listeners.addOnClickListener(onClickListener);
		
	}
	
	public void addOnLongClickListener(OnLongClickListener onClickListener) {
		longListeners.addOnClickListener(onClickListener);
		
	}
	
	public void setState(Boolean state){
		selected=state;
		btn.setSelected(selected);
	}
	
	public boolean isSelected(){
		return selected;
	}

}
